package spring.boot.btc.model;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import spring.boot.btc.utils.BaseUtils;
import spring.boot.btc.utils.HashUtils;
import spring.boot.btc.utils.RsaUtils;

import java.security.Key;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Map;

/**
 * 比特币钱包
 */
@Slf4j
@Data
public class Wallet {

    private static final Wallet WALLET = new Wallet();

    /**
     * 相当于账户
     */
    private PublicKey publicKey;

    /**
     * 相当于密码
     */
    private PrivateKey privateKey;

    private Wallet() {
        Map<String, Key> keyMap = RsaUtils.initKey();
        this.publicKey = (PublicKey) keyMap.get(RsaUtils.PUBLIC_KEY);
        this.privateKey = (PrivateKey) keyMap.get(RsaUtils.PRIVATE_KEY);
    }

    public static Wallet getInstance() {
        return WALLET;
    }


    public String getAddress() {
        if (publicKey == null) {
            return null;
        }
        return HashUtils.sha256(BaseUtils.toBase64(publicKey.getEncoded()));
    }


    /**
     * 取得公钥
     */
    public String getPublicKeyString() {
        return BaseUtils.toBase64(publicKey.getEncoded());
    }
}
